var BookDetail = function(id, name, author, price, des,imageUrl) {
	this.Id=id;
	this.Name = name;
	this.Author = author;
	this.Des = des;
	this.Price= price;
	this.imageUrl = imageUrl;
}

var Comment = function(avatar, userName, comment, time) {
	this.Avatar = avatar;
	this.UserName = userName;
	this.Comment = comment; 
	this.Time = time;
}

var Product = function(book, comments) {
	this.BookDetail = book;
	this.Comments = comments;
}

function getProductDetail(event) {
	//var model = new ProductDetailModel().createModel();
	//pagesManager.setPage(Pages.productDetail, true,model);
	//alert(event.currentTarget.id);
	
	ajax.get({
		url : "http://localhost:8080/book-service/public/book/store/detail?",
		param : {
			"bookID" : event.currentTarget.id
		},
		success : function(data, xhr) {
			var title = data.getElementsByTagName("bookname")[0].childNodes[0].nodeValue;
			var author = data.getElementsByTagName("author")[0].childNodes[0].nodeValue;
			var price = data.getElementsByTagName("price")[0].childNodes[0].nodeValue;
			var des = data.getElementsByTagName("description")[0].childNodes[0].nodeValue;

			var bookId = data.getElementsByTagName("Book")[0].getAttribute("bookID");
			var image = data.getElementsByTagName("bookImage")[0].childNodes[0].nodeValue; 
			var book = new BookDetail(bookId, title, author, price, des,image);

			var commentListServer = data.getElementsByTagName("comment");

			var comments = [];

			for (var i = 0; i < commentListServer.length; i++) {
				console.log(commentListServer[i]);
				var avatar = commentListServer[i].getElementsByTagName("userAvatar")[0].childNodes[0].nodeValue;
				var userName = commentListServer[i].getElementsByTagName("userName")[0].childNodes[0].nodeValue;
				var comment = commentListServer[i].getElementsByTagName("content")[0].childNodes[0].nodeValue;
				var createTime = commentListServer[i].getElementsByTagName("createTime")[0].childNodes[0].nodeValue;
				comments[i] = new Comment(avatar, userName, comment, createTime);
			};

			var model = new Product(book, comments);
			showDetailView(model);
			showPage("detailView",true);
            $id("addToCart").data = book;
            $id("addToCart").addEventListener("click", addToCart, false);
            disableAddToCart(book);
			//pagesManager.setPage(Pages.productDetail, true, model);
			///console.log(data);
			// alert(data.getElementsByTagName("content")[0].childNodes[0].nodeValue);
			//data.getElement
		},
		failure : function(xhr) {
			if(xhr.status==404){
				alert("Xin lỗi bạn. Cuốn sách bạn định xem vừa bị xóa hoặc không tìm thấy");
				load();
			}
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});
};
function addComment(){
		  	var textbox = document.getElementById("txtCmt");
	  		var commentText = textbox.value;
	  		var bookId = textbox.getAttribute("bookid");
	  		if(commentText){
				ajax.put({
		            url : "http://localhost:8080/book-service/public/book/store/comment/add",
		            param : {
		                "bookID" : bookId,
		                "comment":commentText
		            },
		            
		            success : function(data, xhr) {
		            	//console.log(data);
	            	 	var avatar = data.getElementsByTagName("userAvatar")[0].childNodes[0].nodeValue;
						var userName = data.getElementsByTagName("userName")[0].childNodes[0].nodeValue;
						var comment = data.getElementsByTagName("content")[0].childNodes[0].nodeValue;
						var createTime = data.getElementsByTagName("createTime")[0].childNodes[0].nodeValue;
						var commentModel = new Comment(avatar, userName, comment, createTime);
						createCommentNode(commentModel);
						updateScrollBar();
		            },
		            failure : function(xhr) {
		            	if(xhr.status==401){
		            		doPleaseLogin();
		            	}
		                console.log("fail");
		            },
		            abort : function(xhr) {
		                callback.abort(xhr);
		            }
		    	});
		    } else {
		    	alert("hãy nhập comment");
		    }
}

function createCommentNode(comment) {
	var rootNode = $id("users");
	var nodeUser = null;
	var nodeAvatar = null;
	var nodeCmtBox = null;
	var nodeUserName = null;
	var nodeUserLink = null;
	var nodeCmt = null;
	var nodeTime = null;
	var cmt = null;
	//var length = arrCmts.length;

	cmt = comment;

	nodeUser = createDiv();
	nodeUser.addClass("user");

	nodeAvatar = createDiv();
	nodeAvatar.addClass("avatar");
	nodeAvatar.style.background = "url(" + cmt.Avatar + ")";

	nodeCmtBox = createDiv();
	nodeCmtBox.addClass("contentCmt");

	nodeUserName = createDiv();
	nodeUserName.addClass("username");

	nodeUserLink = createNode("a");
	nodeUserLink.setText(cmt.UserName);

	nodeCmt = createDiv();
	nodeCmt.addClass("comment");
	nodeCmt.setText(cmt.Comment);

	nodeTime = createDiv();
	nodeTime.addClass("time");
	nodeTime.setText(cmt.Time);

	nodeUserName.appendChild(nodeUserLink);

	nodeCmtBox.appendChild(nodeUserName);
	nodeCmtBox.appendChild(nodeCmt);
	nodeCmtBox.appendChild(nodeTime);

	nodeUser.appendChild(nodeAvatar);
	nodeUser.appendChild(nodeCmtBox);

	rootNode.appendChild(nodeUser);

	document.getElementById("txtCmt").value = "";

}

function showDetailView(data){
	$id("detailTitle").setText(data.BookDetail.Name);
		
	$id("detailAuthor").setText(data.BookDetail.Author);
	$id("detailContent").setText(data.BookDetail.Des);
	$id("detailPrice").setText(data.BookDetail.Price);
	$id("txtCmt").setAttribute("bookID", data.BookDetail.Id);
	$id("detailImg").src = data.BookDetail.imageUrl;
	createCommentsNode(data.Comments); 

}

function createCommentsNode(arrCmts){
			var rootNode = $id("users");
		var nodeUser = null;
		var nodeAvatar = null;
		var nodeCmtBox = null;
		var nodeUserName = null;
		var nodeUserLink = null;
		var nodeCmt = null; 
		var nodeTime = null;
		var cmt = null;
		var length = arrCmts.length; 		
 		
 		rootNode.innerHTML = "";
 		
		for(var i=0;i<length;i++) {
			cmt = arrCmts[i];
			
			nodeUser = createDiv();
			nodeUser.addClass("user");
			
			nodeAvatar = createDiv();
			nodeAvatar.addClass("avatar");
			nodeAvatar.style.background = "url(" + cmt.Avatar + ")";
			
			nodeCmtBox = createDiv();
			nodeCmtBox.addClass("contentCmt");
			
			nodeUserName = createDiv();
			nodeUserName.addClass("username");
			
			nodeUserLink = createNode("a");
			nodeUserLink.setText(cmt.UserName);			
			
			nodeCmt = createDiv();
			nodeCmt.addClass("comment");
			nodeCmt.setText(cmt.Comment);
			
			nodeTime = createDiv();
			nodeTime.addClass("time");
			nodeTime.setText(cmt.Time);
			
			nodeUserName.appendChild(nodeUserLink);
			
			nodeCmtBox.appendChild(nodeUserName);
			nodeCmtBox.appendChild(nodeCmt);
			nodeCmtBox.appendChild(nodeTime);
			
			nodeUser.appendChild(nodeAvatar);
			nodeUser.appendChild(nodeCmtBox);
			
			rootNode.appendChild(nodeUser);			
		 }	
}

function updateScrollBar(){
	$id("users").scrollTop = $id("users").scrollHeight;
}

function disableAddToCart(book) {
    var isDisable = CartFunction.checkItemInCart(book);
    if(isDisable) {
        $id("addToCart").setText("Added this book to your cart");
    } else {
        $id("addToCart").setText("Add to cart");
    }
}

function addToCart(event){
	if(!CartFunction.checkItemInCart(event.currentTarget.data)) {
        CartFunction.addToCart(event.currentTarget.data);
        disableAddToCart(event.currentTarget.data);
    }
}
